//
//  LoginRegisterIdentityView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/7.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Reusable
import Localize_Swift

extension LoginRegisterIdentityView: NibLoadable {}

class LoginRegisterIdentityView: UIView {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionLayoutHeight: NSLayoutConstraint!
    
    @IBOutlet weak var chineseRequiredLabel: UILabel!
    @IBOutlet weak var ruRequiredLabel: UILabel!
    
    var list: [IdentityModel] = IdentityModel.dataSources()
    var didSelectedIdentityBlock: ((IdentityModel) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        initUI()
        /// 监听语言的变化
        NotificationCenter.default.addObserver(self, selector: #selector(updateLanguageAction), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
        updateLanguageAction()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 刷新 updateCollectionView(), 要在这里更新，防止获取 CollectionView 的 width 不准确
        updateLanguageAction()
    }
    
    // 更新语言
    @objc private func updateLanguageAction() {
       
        titleLabel.text = "选择身份".localized(.login)
        
        if LanguageManager.isLanguageZH {
            chineseRequiredLabel.isHidden = false
            ruRequiredLabel.isHidden = true
            
            //            let layout = UICollectionViewFlowLayout()
            //            layout.scrollDirection = .vertical
            //            layout.minimumLineSpacing = 8
            //            layout.minimumInteritemSpacing = 8
            //            let itemW: CGFloat = (xScreenW - 24*2 - 14*2)/3
            //            layout.itemSize = CGSize(width: itemW, height: 32)
            //            collectionView.setCollectionViewLayout(layout, animated: true)
            
        } else {
            chineseRequiredLabel.isHidden = true
            ruRequiredLabel.isHidden = false
            
            //            let layout = YLCollectionViewFlowLayout()
            //            layout.delegate = self
            //            layout.columnSpacing = 8
            //            layout.rowSpacing = 8
            //            collectionView.setCollectionViewLayout(layout, animated: true)
        }
        
        updateCollectionView()
    }
}

private extension LoginRegisterIdentityView {
    
    func initUI() {
        chineseRequiredLabel.addRoundedCorners(radius: 8)
        backgroundColor = .clear
        
        //        let layout = UICollectionViewFlowLayout()
        //        layout.scrollDirection = .vertical
        //        layout.minimumLineSpacing = 8
        //        layout.minimumInteritemSpacing = 8
        //        let itemW: CGFloat = (xScreenW - 24*2 - 14*2)/3
        //        layout.itemSize = CGSize(width: itemW, height: 32)
        
        let layout = YLCollectionViewFlowLayout()
        layout.delegate = self
        layout.columnSpacing = 8
        layout.rowSpacing = 8
        collectionView.setCollectionViewLayout(layout, animated: true)
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.isScrollEnabled = false
        collectionView.bounces = false
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(cellType: LoginRegisterIdentityCell.self)
    }
    
    func updateCollectionView() {
        collectionView.reloadData()
        collectionView.layoutIfNeeded()
        
        let collectionViewHeight = collectionView.contentSize.height
        collectionLayoutHeight.constant = collectionViewHeight
        
    }
    
}

extension LoginRegisterIdentityView: UICollectionViewDelegate, UICollectionViewDataSource, YLCollectionFlowLayoutDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: LoginRegisterIdentityCell.self)
        cell.model = list[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        list.forEach { model in
            model.isSelected = false
        }
        
        let model = list[indexPath.row]
        model.isSelected = true
        collectionView.reloadData()
        didSelectedIdentityBlock?(model)
    }
    
    func collectionView(sizeForItemAt index: Int) -> CGSize {
        let model = list[index]
        var width = model.name.localized(.login).sg.size(width: CGFLOAT_MAX, font: .systemFont(ofSize: 12)).width
        
        width += CGFloat(32)
        width = ceil(width)
        width = min(xScreenW - 24*2, width)
        return CGSizeMake(width, 32)
    }
}
